package com.software.lease.common.exception;

import com.software.lease.common.result.Result;
import com.software.lease.common.result.ResultCodeEnum;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice//声明处理全局controller类异常的方法
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)//声明处理异常的方法, value表示要处理的异常类型
    @ResponseBody//将方法的返回值作为http的返回体
    public Result error(Exception e){
        e.printStackTrace();
        ResultCodeEnum resultCodeEnum = ResultCodeEnum.getEnumByMessage(e.getMessage());
        return Result.fail(resultCodeEnum);
    }
}
